package partner

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
	"time"
)

//渠道方面的

//根据union_id查找玩家与代理(或玩家)的关系
func AgentPlayerByUnionId(unionId string, multiOrm ...orm.Ormer) (AgentPlayerRelation, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			union_id = ?
		LIMIT 1
		`, TableAgentPlayer)
	o := NewOrm(multiOrm, DBChess)
	var p AgentPlayerRelation
	err := o.Raw(sql, unionId).QueryRow(&p)
	if CheckNoExist(err) {
		return p, nil
	}
	return p, err
}

func NewAgentPlayerRelation(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	values, sql := InsertSql(TableAgentPlayer, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateAgentPlayerRelation(updateData map[string]interface{}, id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TableAgentPlayer, updateData, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//更新代理的group_id,root_id
func UpdateAgentPlayer(updateData map[string]interface{}, agentUid int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	condition := fmt.Sprintf(`agent_uid = %v`, agentUid)
	values, sql := UpdateSql(TableAgentPlayer, updateData, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func UpdateAgentPlayerRelationByUnionId(playerUid int64, unionId string, multiOrm ...orm.Ormer) {
	o := NewOrm(multiOrm, DBChess)
	data := map[string]interface{}{
		"player_uid":    playerUid,
		"activate_time": time.Now().Unix(),
	}
	condition := fmt.Sprintf(`union_id = '%v'`, unionId)
	values, sql := UpdateSql(TableAgentPlayer, data, condition)
	_, err := o.Raw(sql, values).Exec()
	if err != nil {
		LogPF.Error(fmt.Sprintf("更新玩家与代理之间的绑定关系失败:%v", err))
	}
}

//列出渠道(用户)与用户之间的关系
func ListAgentPlayerRelation(p ListParams, agentUid int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			ap.*,u.nickname,u.imageurl,u.logintime
        FROM
			%v AS ap
		LEFT JOIN
			%v AS u
		ON u.uid = ap.player_uid
		WHERE
			ap.agent_uid = %v
		ORDER BY
			ap.id
		LIMIT ?,?
		`, TableAgentPlayer, TableClientUser, agentUid)
	o := NewOrm(multiOrm, DBChess)
	var ap []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&ap)
	if CheckError(err) {
		return ap, 0, err
	}
	var count int64
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE agent_uid = %v`, TableAgentPlayer, agentUid)
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return ap, 0, err
	}
	return ap, count, nil
}

//列出合伙人名下所有玩家
func ListGroupPlayer(p ListParams, groupId int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			ap.*,u.nickname,u.imageurl,u.logintime
        FROM
			%v AS ap
		LEFT JOIN
			%v AS u
		ON u.uid = ap.player_uid
		WHERE
			ap.group_id = %v
		ORDER BY
			ap.id
		LIMIT ?,?
		`, TableAgentPlayer, TableClientUser, groupId)
	o := NewOrm(multiOrm, DBChess)
	var ap []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&ap)
	if CheckError(err) {
		return ap, 0, err
	}
	var count int64
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE group_id = %v`, TableAgentPlayer, groupId)
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return ap, 0, err
	}
	return ap, count, nil
}

//返回玩家所属的合伙人
func GetPlayerGroupId(uid int64, multiOrm ...orm.Ormer) int64 {
	sql := fmt.Sprintf(`
		SELECT
			group_id
        FROM
			%v
		WHERE
			 player_uid = %v
		LIMIT 1
		`, TableAgentPlayer, uid)
	o := NewOrm(multiOrm, DBChess)
	var groupId int64
	o.Raw(sql).QueryRow(&groupId)
	return groupId
}
